package webStudio7i.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import webStudio7i.excepcion.DAOExcepcion;
import webStudio7i.modelo.Servicio;
import webStudio7i.negocio.ServiciosBll;

/**
 * Servlet implementation class ServicioServlet
 */
public class ServicioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServicioServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//TODO Auto-generated method stub
		String descripcion = request.getParameter("txtDescripcion");
		String nombre = request.getParameter("txtNombre");
		double precio = Double.parseDouble(request.getParameter("txtPrecio"));
		try {
			// Validando la existencia de un servicio
			ServiciosBll objServBll = new ServiciosBll();
			int c = objServBll.ConsultarServicio(nombre, descripcion);
			if (c != 1 ){
				Servicio objServ = new Servicio(); 
				objServ.setNombre(nombre);
				objServ.setDescripcion(descripcion);
				objServ.setPrecio(precio);
				objServ.setIdusuario_actualizacion(1);
				objServBll.insertarServicio(objServ);
				//response.sendRedirect("registro_servicios.jsp");
				request.setAttribute("rpta", "1");
				RequestDispatcher rd = request.getRequestDispatcher("/registro_servicios.jsp");
				rd.forward(request, response);
				return;
			}else {
				request.setAttribute("rpta", "0");
				RequestDispatcher rd = request.getRequestDispatcher("/registro_servicios.jsp");
				rd.forward(request, response);
				return;
			}
		} catch (DAOExcepcion e) {
			e.printStackTrace();
		}
	}

}
